package handler;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class FinanceChainHandler {
	
	private static String cfgpath = "D:/javasdkdemo/config/bcs-function-channel-sdk-config.yaml";
	
    /**
     * 通用数据上链
     * @param orgid
     * 	操作组织ID
     * @param dataname
     * 	数据name
     * @param datakey
     * 	数据key
     * @param data
     * 	数据json
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{"transactionid":"25fe8572542ce04da522be3d5aea4304e0c866aebad6d157b605f655ae74a514"}}
     *  error非零时，返回msg，错误信息
     */
    public String saveGenericRecord(String orgid, String dataname,String datakey,String data){
    	String keydata = getKeyType(orgid,dataname);
    	
    	if(StringUtils.isNoneEmpty(keydata) && JSON.parseObject(keydata).getIntValue("error") != 0){
    		setKeyType(orgid, dataname, "simple");
    	}
    	String transactiondata = fabricOrg(orgid).invokeBlockchain("saveRecord", ArrayUtils.addAll(new String[]{dataname}, JSON.toJSONString(new String[]{datakey}), data));
    	return transactiondata;
    }
    
    /**
     * 通用数据链上查询
     * @param orgid
     * 	查询组织ID
     * @param dataname
     * 	数据name
     * @param datakey
     * 	数据key
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{record}}
     *   error非零时，返回msg，错误信息
     */
	public String getGenericRecord(String orgid, String dataname, String datakey) {
		String data = FabricHelper.getInstance().setAccessKey(orgid).
				queryBlockchain("queryRecord", ArrayUtils.addAll(new String[]{dataname}, JSON.toJSONString(new String[]{datakey})));
		return data;
	}

	/**
	 * 财务数据写(有固定的表，只能从这里写)
	 * @param orgid
     * 	操作组织ID
     * @param datakey
     * 	数据key
     * @param data
     * 	数据json
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{"transactionid":"25fe8572542ce04da522be3d5aea4304e0c866aebad6d157b605f655ae74a514"}}
     *  error非零时，返回msg，错误信息
     */
    public String saveFinancialRecord(String orgid, String datakey,String data){
    	String keydata = getKeyType(orgid,"financial");
    	
    	if(StringUtils.isNoneEmpty(keydata) && JSON.parseObject(keydata).getIntValue("error") != 0){
    		setKeyType(orgid, "financial", "simple");
    	}
    	String transactiondata = FabricHelper.getInstance().setAccessKey(orgid).
    			invokeBlockchain("saveRecord", ArrayUtils.addAll(new String[]{"financial"}, JSON.toJSONString(new String[]{datakey}), data));
    	return transactiondata;
    }

	/**
	 * 财务数据读(有固定的表，只能从这里读)
	 * @param orgid
     * 	查询组织ID
     * @param datakey
     * 	数据key
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{record}}
     *   error非零时，返回msg，错误信息
     */
    public String getFinancialRecord(String orgid, String datakey){
		String data = FabricHelper.getInstance().setAccessKey(orgid).
				queryBlockchain("queryRecord", ArrayUtils.addAll(new String[]{"financial"}, JSON.toJSONString(new String[]{datakey})));
		return data;
    }
    
    /**
     * 写文件
     * @param orgid
     * 	操作组织ID
     * @param path
     * 	文件全路径
     * @param hashcode
     * 	文件hashcode
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{"transactionid":"25fe8572542ce04da522be3d5aea4304e0c866aebad6d157b605f655ae74a514"}}
     *  error非零时，返回msg，错误信息
     */
    public String saveFileRecord(String orgid, String path,String hashcode){
    	String keydata = getKeyType(orgid,"financefile");
    	
    	if(StringUtils.isNoneEmpty(keydata) && JSON.parseObject(keydata).getIntValue("error") != 0){
    		setKeyType(orgid, "financefile", "simple");
    	}
    	JSONObject data = new JSONObject();
    	data.put("hashcode", hashcode);
    	String transactiondata = FabricHelper.getInstance().setAccessKey(orgid).
    			invokeBlockchain("saveRecord", ArrayUtils.addAll(new String[]{"financefile"}, JSON.toJSONString(new String[]{path}), data.toJSONString()));
    	return transactiondata;
    }
    
    /**
     * 读文件
     * @param orgid
     * 	查询组织ID
     * @param path
     * 	文件全路径
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{record}}
     *   error非零时，返回msg，错误信息
     */
    public String getFileRecord(String orgid, String path){
		String data = FabricHelper.getInstance().setAccessKey(orgid).
				queryBlockchain("queryRecord", ArrayUtils.addAll(new String[]{"financefile"}, JSON.toJSONString(new String[]{path})));
		return data;
    }
    
    /**
     * get key type of the object data, simple or composite
     * @param orgid
     * 	查询组织ID
     * @param dataname
     * 	数据name
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{simple or composite}}
     *   error非零时，返回msg，错误信息
     */
    public String getKeyType(String orgid, String dataname){
    	return FabricHelper.getInstance().setAccessKey(orgid).queryBlockchain("getKeyType", new String[]{dataname});
    }
    
    /**
     * set key type of the object data, simple or composite
     * @param orgid
     * 	查询组织ID
     * @param dataname
     * 	数据name
     * @return
     *  jsonstring ：{"error":0,"msg":"error msg","data":{transactionid}}
     *   error非零时，返回msg，错误信息
     */
    public String setKeyType(String orgid, String dataname, String keyData){
    	return FabricHelper.getInstance().setAccessKey(orgid).invokeBlockchain("setKeyType", new String[]{dataname, keyData});
    }
	
	private static FabricHelper helper = FabricHelper.getInstance();
	
	public FinanceChainHandler() {
		helper.setConfigCtx(cfgpath);
	}

	public FabricHelper fabricOrg(String orgid){
    	return helper.setAccessKey(orgid);
	}
	
	private static class Holder {
		private static FinanceChainHandler instance = new FinanceChainHandler();
	}

	public static FinanceChainHandler getInstance() {
		return Holder.instance;
	}
    
}
